package com.why.whyoj.judge;

import cn.hutool.json.JSONUtil;
import com.why.whyoj.common.ErrorCode;
import com.why.whyoj.exception.BusinessException;
import com.why.whyoj.judge.codesanbox.CodeSandBox;
import com.why.whyoj.judge.codesanbox.CodeSandBoxFactory;
import com.why.whyoj.judge.codesanbox.CodeSandBoxProxy;
import com.why.whyoj.judge.codesanbox.model.ExecuteCodeRequest;
import com.why.whyoj.judge.codesanbox.model.ExecuteCodeResponse;
import com.why.whyoj.judge.strategy.DefaultJudgeStrategy;
import com.why.whyoj.judge.strategy.JudgeContext;
import com.why.whyoj.model.dto.question.JudgeCase;
import com.why.whyoj.model.dto.question.JudgeConfig;
import com.why.whyoj.model.dto.questionsubmit.JudgeInfo;
import com.why.whyoj.model.entity.Question;
import com.why.whyoj.model.entity.QuestionSubmit;
import com.why.whyoj.model.enums.JudgeInfoMessageEnum;
import com.why.whyoj.model.enums.QuestionSubmitLauguageEnum;
import com.why.whyoj.model.enums.QuestionSubmitStatusEnum;
import com.why.whyoj.model.vo.QuestionSubmitQueryVO;
import com.why.whyoj.service.QuestionService;
import com.why.whyoj.service.QuestionSubmitService;
import org.jetbrains.annotations.TestOnly;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class JudgeServiceImpl implements JudgeService {
    @Value("${codesandbox.type: remote}")
    String type;
    @Resource
    QuestionSubmitService questionSubmitService;
    @Resource
    QuestionService questionService;

    @Resource
    JudgeManager judgeManager;

    @Override
    public QuestionSubmit doJudge(long questionSubmitId) {
        QuestionSubmit questionSubmit = questionSubmitService.getById(questionSubmitId);
        if (questionSubmit == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "请求的提交记录不存在");
        }
        Question question = questionService.getById(questionSubmit.getQuestionId());
        if (question == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "题目不存在");
        }
        if (!questionSubmit.getStatus().equals(QuestionSubmitStatusEnum.WATTING.getValue())) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "题目状态更新错误");
        }
        QuestionSubmit questionSubmitUpdate = new QuestionSubmit();
        questionSubmitUpdate.setId(questionSubmitId);
        questionSubmitUpdate.setStatus(QuestionSubmitStatusEnum.RUNTING.getValue());
        boolean update = questionSubmitService.updateById(questionSubmitUpdate);
        if (!update) throw new BusinessException(ErrorCode.SYSTEM_ERROR, "题目提交状态更新失败");
        CodeSandBox codeSandBox = CodeSandBoxFactory.newInstance(type);
        codeSandBox = new CodeSandBoxProxy(codeSandBox);
        List<JudgeCase> judgeCases = JSONUtil.toList(question.getJudgeCase(), JudgeCase.class);
        List<String> inputList = judgeCases.stream().map(JudgeCase::getInput).collect(Collectors.toList());
        String language = QuestionSubmitLauguageEnum.JAVA.getValue();
        String code = questionSubmit.getCode();
        ExecuteCodeRequest request = ExecuteCodeRequest.builder()
                .code(code)
                .language(language)
                .inputList(inputList)
                .build();
        ExecuteCodeResponse response = codeSandBox.executeCode(request);
        JudgeConfig judgeConfig = JSONUtil.toBean(question.getJudgeConfig(), JudgeConfig.class);
        JudgeContext judgeContext = new JudgeContext(questionSubmit.getLanguage(),judgeCases, judgeConfig, response);
        JudgeInfo judgeInfo = judgeManager.doJudge(judgeContext);
        QuestionSubmit questionSubmitUpdate1 = new QuestionSubmit();
        questionSubmitUpdate1.setId(questionSubmitId);
        questionSubmitUpdate1.setStatus(response.getStatus());
        questionSubmitUpdate1.setJudgeInfo(JSONUtil.toJsonStr(judgeInfo));
        boolean update1 = questionSubmitService.updateById(questionSubmitUpdate1);
        if (!update1) throw new BusinessException(ErrorCode.SYSTEM_ERROR, "题目提交状态更新失败");
        QuestionSubmit questionSubmit1 = questionSubmitService.getById(questionSubmitId);
        return questionSubmit1;

    }


}

